package com.astar.service.impl;

import com.astar.domain.UnifiedUserInfo;
import com.astar.enums.EnumUserInfo;
import com.astar.mapper.UnifiedUserInfoMapper;
import com.astar.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * Created by liuruiyan on 2019/8/25.
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UnifiedUserInfoMapper,UnifiedUserInfo> implements UserInfoService {


    @Autowired
    private UnifiedUserInfoMapper unifiedUserInfoMapper;


    @Override
    public UnifiedUserInfo queryInfoByUnionId(String unionId) {

        List<UnifiedUserInfo> list = null;
        if(StringUtils.isBlank(unionId)) {
            return null;
        }
        try {
            list = unifiedUserInfoMapper.queryInfoByUnionId(unionId);
            if(list!=null && list.isEmpty()){
                return list.get(0);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void insert(UnifiedUserInfo unifiedUserInfo) {
        unifiedUserInfo.setCreatedTime(LocalDateTime.now());
        unifiedUserInfoMapper.insert(unifiedUserInfo);
    }

    @Override
    public void update(UnifiedUserInfo unifiedUserInfo) {
        unifiedUserInfo.setUpdatedTime(LocalDateTime.now());
        unifiedUserInfoMapper.updateByExampleSelective(unifiedUserInfo,null);
    }

    @Override
    public UnifiedUserInfo queryInfoAndSave(UnifiedUserInfo unifiedUserInfo) {
        UnifiedUserInfo result = null;
        try {
            result = this.queryInfoByUnionId(unifiedUserInfo.getUnionid());
        } catch (Exception e) {
            e.printStackTrace();
        }

        if(result==null){
            this.insert(unifiedUserInfo);
        }

        try {
            result = this.queryInfoByUnionId(unifiedUserInfo.getUnionid());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

    /**
     * 更新指定列内容
     * @param userId
     * @param columnName
     * @param value
     */
    @Override
    public void updateByColumn(Integer userId, String columnName, String value) {
        UnifiedUserInfo unifiedUserInfo = new UnifiedUserInfo();
        unifiedUserInfo.setId(userId);
        unifiedUserInfo.setUpdatedTime(LocalDateTime.now());
        if(EnumUserInfo.USERNAME.getCode().equals(columnName)){
            unifiedUserInfo.setUsername(value);
        }else if(EnumUserInfo.CITY.getCode().equals(columnName)){
            unifiedUserInfo.setNickname(value);
        }else if(EnumUserInfo.SCHOOL.getCode().equals(columnName)){
            unifiedUserInfo.setShcool(value);
        }else if(EnumUserInfo.AGE.getCode().equals(columnName)){
            unifiedUserInfo.setAge(Integer.parseInt(value));
        }else if(EnumUserInfo.GRADE.getCode().equals(columnName)){
            unifiedUserInfo.setGrade(value);
        }

        unifiedUserInfoMapper.updateById(unifiedUserInfo);
    }

}
